# delimit ;
clear ;
cd "replication" ;
set more off ; 

* ***************************************************************************** ;
* MDE calculation
* ***************************************************************************** ;

* first, we need to collect all the estimates from the main tables ; 
* make dataset to keep the estimates  ;

clear ; 
set obs 1000 ;

gen table = "" ;
gen outcome = "" ;
gen coefficient = "" ;
gen estimate = . ; 
gen se = . ; 
gen ycontrolsd = . ; 

local i = 1 ;
save temp, replace ; 

* ***************************************************************************** ;
* collect results from midline knowledge 
* ***************************************************************************** ;

cd ".." ;
do "./replication/do/table03-midline-knowledge-aggregate.do" ;

* regressions: agggregate scores ; 
foreach var of varlist score_tot score_cleanliness score_midwife score_condom { ; 

	areg `var' healthonly healthandpay, a(strata) robust ;
	
	summ `var' if healthonly == 0 & healthandpay == 0 & e(sample) ;
	local ycontrolsd = r(sd) ;
	
	preserve ;
	use temp, clear ;
	
	replace table = "midline knowledge" in `i' ;
	replace outcome = "`var'" in `i' ;
	replace coefficient = "HEEC-HEE" in `i' ; 
	
	lincom healthonly-healthandpay ;
	replace estimate = r(estimate) in `i' ;
	replace se = r(se) in `i' ; 
	replace ycontrolsd = `ycontrolsd' in `i' ;
	local ++ i ;

	save , replace ;
	restore ;
} ;

* ***************************************************************************** ;
* collect results from endline knowledge
* ***************************************************************************** ;

cd ".." ;
do "./replication/do/table04-endline-knowledge-aggregate.do" ;

foreach var in module_all module1_know module23_know module4_know module5_know { ;
	
	areg `var' healthonly healthandpay, a(strata) robust ;
	
	summ `var' if healthonly == 0 & healthandpay == 0 & e(sample) ;
	local ycontrolsd = r(sd) ;
		
	preserve ;
	use temp, clear ;
	
	replace table = "endline knowledge" in `i' ;
	replace outcome = "`var'" in `i' ;
	replace coefficient = "HEEC-HEE" in `i' ; 
	
	lincom healthonly-healthandpay ;
	replace estimate = r(estimate) in `i' ;
	replace se = r(se) in `i' ; 
	replace ycontrolsd = `ycontrolsd' in `i' ;
	local ++ i ;
	
	save , replace ;
	restore ;

} ;

* ***************************************************************************** ;
* make table for output, doing this manually 
* ***************************************************************************** ;

use temp, clear ; 

* make the columns ;
gen coeff = "" ; 
replace coeff = "\textit{Panel A. Effects on Short-Term Health Knowledge}" in 1 ;
replace coeff = "Control Group, SD" in 2 ;
replace coeff = "Marginal Effect of Cash Incentives, SE" in 3; 
replace coeff = "Marginal Effect of Cash Incentives, MDE" in 4; 
replace coeff = "Marginal Effect of Cash Incentives, MDE \(\div\) Control Group SD" in 5; 

replace coeff = "\\\textit{Panel B. Effects on Longer-Term Health Knowledge}" in 6 ;
replace coeff = "Control Group, SD" in 7; 
replace coeff = "Marginal Effect of Cash Incentives, SE" in 8;
replace coeff = "Marginal Effect of Cash Incentives, MDE" in 9;
replace coeff = "Marginal Effect of Cash Incentives, MDE \(\div\) Control Group SD" in 10; 

forvalues i = 1/5 { ;
	gen col`i' = "" ;
	label var col`i' "(`i')" ;
} ;

* fill in the columns ;
* Panel A ; 
local colnum = 1 ;
foreach y in score_tot score_cleanliness score_midwife score_condom { ;

	summ se if outcome == "`y'" ;
	assert r(N) == 1 ;
	local se = r(mean) ; 

	summ ycontrolsd if outcome == "`y'" ;
	assert r(N) == 1 ;
	local ysd = r(mean) ; 

	replace col`colnum' = string(`ysd', "%9.3f") in 2 ; 
	replace col`colnum' = string(`se', "%9.3f") in 3 ; 
	replace col`colnum' = string(`=`se'*2.8', "%9.3f") in 4 ;
	replace col`colnum' = string(`=`se'*2.8/`ysd'', "%9.3f") in 5 ;

	local ++colnum ;
} ;

* Panel B ; 
local colnum = 1 ;
foreach y in module_all module1_know module23_know module4_know module5_know { ;
	
	summ se if outcome == "`y'" ;
	assert r(N) == 1 ;
	local se = r(mean) ; 

	summ ycontrolsd if outcome == "`y'" ;
	assert r(N) == 1 ;
	local ysd = r(mean) ; 

	replace col`colnum' = string(`ysd', "%9.3f") in 7 ; 
	replace col`colnum' = string(`se', "%9.3f") in 8 ; 
	replace col`colnum' = string(`=`se'*2.8', "%9.3f") in 9 ;
	replace col`colnum' = string(`=`se'*2.8/`ysd'', "%9.3f") in 10 ;

	local ++colnum ;
} ;

* output the table ;
keep coeff col1-col5 ;
dropmiss, obs force ;

texsave using "./output/appendix-table-mde.tex",
	replace frag nofix
	title("Minimum Detectable Effect Size")
	marker(table-mde)
	varlabels
	align(lCCCCC)
	width(0.75\linewidth)
	footnote("\textit{Notes:} 
	The table presents the ex-post Minimum Detectable Effect (MDE) sizes for
	the marginal effect of cash incentives, assuming 80 percent 
	power and the 5 percent significance level. 
	The marginal effect of cash incentives is the effect of HEEC minus the effect of HEE (i.e., $\beta_2 - \beta_1$ in Equation \ref{reg-equation}).
	The MDEs are obtained as 2.8 times the 
	standard error (SE) of the estimate, and then expressed as a proportion 
	of the control group standard deviation (SD). 
	Panels A and B correspond to 
	Tables \ref{table-midline-knowledge-aggregate} and \ref{table-endline-knowledge-aggregate}, respectively.
	For example, in Panel A, Column 1, the value 0.012 is the SE of 
	$\beta_2 - \beta_1$ in Table \ref{table-midline-knowledge-aggregate}, Column 1.", 
	width(p{0.75\linewidth})) ;

* cleanup table footer ;
filefilter "./output/appendix-table-mde.tex" "./output/appendix-table-mde-v00.tex", from("\BSmulticolumn{6}{p{0.75\BSlinewidth}}{\BSbegin{footnotesize}") to("\BScaption*{\BSfootnotesize") replace ;
filefilter "./output/appendix-table-mde-v00.tex" "./output/appendix-table-mde-v01.tex", from("\BSend{footnotesize}}") to("}") replace ;
filefilter "./output/appendix-table-mde-v01.tex" "./output/appendix-table-mde-v02.tex", from("\BSend{tabularx}") to("") replace ;
filefilter "./output/appendix-table-mde-v02.tex" "./output/appendix-table-mde.tex", from("\BSbottomrule \BSaddlinespace[\BSbelowrulesep]") to("\BSbottomrule \BSend{tabularx} \BScaptionsetup{width=0.75\BSlinewidth}") replace ;

erase "temp.dta" ; 
forvalues i = 0/2 { ;
	erase "./output/appendix-table-mde-v0`i'.tex" ;
} ;

exit ; 
